package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysBuildUnit;
import com.ruoyi.system.mapper.SysBuildBuildingMapper;
import com.ruoyi.system.domain.SysBuildBuilding;
import com.ruoyi.system.service.ISysBuildBuildingService;

/**
 * 楼层管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-28
 */
@Service
public class SysBuildBuildingServiceImpl implements ISysBuildBuildingService 
{
    @Autowired
    private SysBuildBuildingMapper sysBuildBuildingMapper;

    /**
     * 查询楼层管理
     * 
     * @param id 楼层管理ID
     * @return 楼层管理
     */
    @Override
    public SysBuildBuilding selectSysBuildBuildingById(Long id)
    {
        return sysBuildBuildingMapper.selectSysBuildBuildingById(id);
    }

    /**
     * 查询楼层管理列表
     * 
     * @param sysBuildBuilding 楼层管理
     * @return 楼层管理
     */
    @Override
    public List<SysBuildBuilding> selectSysBuildBuildingList(SysBuildBuilding sysBuildBuilding)
    {
        return sysBuildBuildingMapper.selectSysBuildBuildingList(sysBuildBuilding);
    }

    /**
     * 新增楼层管理
     * 
     * @param sysBuildBuilding 楼层管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysBuildBuilding(SysBuildBuilding sysBuildBuilding)
    {
        int rows = sysBuildBuildingMapper.insertSysBuildBuilding(sysBuildBuilding);
        insertSysBuildUnit(sysBuildBuilding);
        return rows;
    }

    /**
     * 修改楼层管理
     * 
     * @param sysBuildBuilding 楼层管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysBuildBuilding(SysBuildBuilding sysBuildBuilding)
    {
        sysBuildBuildingMapper.deleteSysBuildUnitByBuildId(sysBuildBuilding.getId());
        insertSysBuildUnit(sysBuildBuilding);
        return sysBuildBuildingMapper.updateSysBuildBuilding(sysBuildBuilding);
    }

    /**
     * 批量删除楼层管理
     * 
     * @param ids 需要删除的楼层管理ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysBuildBuildingByIds(Long[] ids)
    {
        sysBuildBuildingMapper.deleteSysBuildUnitByBuildIds(ids);
        return sysBuildBuildingMapper.deleteSysBuildBuildingByIds(ids);
    }

    /**
     * 删除楼层管理信息
     * 
     * @param id 楼层管理ID
     * @return 结果
     */
    @Override
    public int deleteSysBuildBuildingById(Long id)
    {
        sysBuildBuildingMapper.deleteSysBuildUnitByBuildId(id);
        return sysBuildBuildingMapper.deleteSysBuildBuildingById(id);
    }

    /**
     * 新增单元管理信息
     * 
     * @param sysBuildBuilding 楼层管理对象
     */
    public void insertSysBuildUnit(SysBuildBuilding sysBuildBuilding)
    {
        List<SysBuildUnit> sysBuildUnitList = sysBuildBuilding.getSysBuildUnitList();
        Long id = sysBuildBuilding.getId();
        if (StringUtils.isNotNull(sysBuildUnitList))
        {
            List<SysBuildUnit> list = new ArrayList<SysBuildUnit>();
            for (SysBuildUnit sysBuildUnit : sysBuildUnitList)
            {
                sysBuildUnit.setBuildid(id);
                list.add(sysBuildUnit);
            }
            if (list.size() > 0)
            {
                sysBuildBuildingMapper.batchSysBuildUnit(list);
            }
        }
    }
}
